package com.edz.from0507to0513apk.View;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.edz.from0507to0513apk.Base.BaseActivity;
import com.edz.from0507to0513apk.R;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import butterknife.BindView;

public class AlipayActivity extends BaseActivity {

    @BindView(R.id.btn_test)
    Button btn_test;
    @BindView(R.id.tv_res)
    TextView responseText;
    @BindView(R.id.btn_openscan)
    Button btn_openscan;

    @Override
    protected void initView() {
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void setEvent() {
        btn_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//         final String strurl = "http://192.168.199.1/goform/CommConfig?cmd=CustInvoke&action=WebRedirectCtl&item=AcceptOneTemp&subitem=34:69:87:c0:e9:45_30";
                final String strurl = "http://www.baidu.com";
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String strurl = "https://www.baidu.com/";
                            URL url1 = new URL(strurl);
                            //得到connection对象。
                            HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
                            //设置请求方式
                            connection.setRequestMethod("GET");
                            //连接
                            connection.connect();
                            //得到响应码
                            int responseCode = connection.getResponseCode();
                            if (responseCode == HttpURLConnection.HTTP_OK) {
                                //得到响应流
                                InputStream inputStream = connection.getInputStream();
                                //将响应流转换成字符串
                                final String result = convertStreamToString(inputStream);//将流转换为字符串。
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        responseText.setText(result);
                                    }
                                });
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

            }
        });


        btn_openscan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                skipAnotherActivity(AlipayActivity.this,RxScannerCodeActivity.class);
            }
        });
    }

    private String convertStreamToString(InputStream inputStream) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "/n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_alipay;
    }
}
